package com.code.wifi.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.code.wifi.listener.WifiReceiverActionListener;

/**
 * Created by ZJM on 2017/3/30.
 * <p>
 * wifi广播接收者 接收的是系统发出的广播
 * <p>
 * 功能
 * 1.接收 WIFI 开/关状态
 * 2.接收 WIFI 扫描结果通知
 * 3.wifi 连接的广播
 */

public class WifiReceiver extends BroadcastReceiver {
    private final static String TAG = WifiManager.class.getSimpleName();
    private final WifiReceiverActionListener listener;//wifi广播动作监听回调接口

    /**
     * 构造器 初始化回调接口
     *
     * @param listener Wifi接收回调接口
     */
    public WifiReceiver(WifiReceiverActionListener listener) {
        this.listener = listener;
    }

    /**
     * Implement the BroadcastReceiver abstract method
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        //判断广播内容
        String action = intent.getAction();
        if (null == listener) {
            return;
        }

        //判断wifi的action信息
        switch (action) {
            case WifiManager.WIFI_STATE_CHANGED_ACTION:
                //wifi 打开或者关闭状态
                handlerWifiState(intent);
                break;
            case WifiManager.SCAN_RESULTS_AVAILABLE_ACTION:
                //扫描的结果
                handlerWifiScanResult();
                break;
            case WifiManager.NETWORK_STATE_CHANGED_ACTION:
                //判断连接状态
                handlerWifiConnectState(intent);
                break;
        }
    }

    /**
     * 根据api。设置此方法来处理wifi的状态。包括打开、打开中..关闭、关闭中..
     *
     * @param intent intent
     */
    private void handlerWifiState(Intent intent) {
        Log.e(TAG, "------CHANGE" + WifiManager.WIFI_STATE_CHANGED_ACTION);
        //获取wifi状态
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
        //根据wifi状态进行处理
        switch (wifiState) {
            //wifi打开状态
            case WifiManager.WIFI_STATE_ENABLED:
                listener.onWifiOpened();
                break;
            //wifi打开中
            case WifiManager.WIFI_STATE_ENABLING:
                listener.onWifiOpening();
                break;
            //wifi关闭状态
            case WifiManager.WIFI_STATE_DISABLED:
                listener.onWifiClosed();
                break;
            //wifi关闭中
            case WifiManager.WIFI_STATE_DISABLING:
                listener.onWifiClosing();
                break;
            //wifi未知状态
            case WifiManager.WIFI_STATE_UNKNOWN:
                Log.i(TAG, "---Wifi是未知状态");
                break;
        }
    }

    /**
     * wifi扫描结束的广播发出时进行处理
     */
    private void handlerWifiScanResult() {
        listener.onWifiScanResultBack();
    }

    /**
     * wifi连接状态发生改变时惊醒处理处理
     *
     * @param intent intent
     */
    private void handlerWifiConnectState(Intent intent) {
        //获取 NetworkInfo——当前连接的网络
        NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != networkInfo && networkInfo.isConnected()) {
            //若网络连接成功，获取 WifiInfo
            WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
            if (null != wifiInfo) {
                //将wifi广播传播出去
                listener.onWifiConnected(wifiInfo);
            }
        }

    }


}
